package com.zslibrary.StateHandle;


import com.zslibrary.Enum.StateDefine;
import com.zslibrary.Enum.StateEventDefine;
import com.zslibrary.Pipe.Pipe;
import com.zslibrary.Protocol.Poll;
import com.zslibrary.Tools.ZSLog;

public class IdleState extends StateHandle {
    private long mLastTimeStamp=System.currentTimeMillis();
    private Pipe mPipe;

    public IdleState(Pipe pipe){
        this.mPipe=pipe;
    }

    @Override
    public @StateDefine.State int getNextHop(@StateEventDefine.StateEvent int event) {
        if(event== StateEventDefine.EVT_NONE){
            if(System.currentTimeMillis()- mLastTimeStamp>1500) {
                if(System.currentTimeMillis()- mLastTimeStamp<3000) {   //进行耗时操作之后应先poll再启动query gate
                    mLastTimeStamp = System.currentTimeMillis();
                    return StateDefine.STATE_GATE_QUERY;
                }else
                    mLastTimeStamp = System.currentTimeMillis();
            }
        }
        else if(event==StateEventDefine.EVT_RESET)
            return StateDefine.STATE_INIT;
        else if(event==StateEventDefine.EVT_EXECUTE)
            return StateDefine.STATE_BUSY;
        else if(event==StateEventDefine.EVT_DISCONNECT)
            return StateDefine.STATE_ERROR;
        return StateDefine.STATE_IDLE;
    }

    @Override
    public @StateEventDefine.StateEvent int handle(){
//        ZSLog.d("idle state");
        return Poll.masterPoll(mPipe);
    }

}
